package render.tree
{
	import mx.controls.Tree;
	import mx.events.ListEvent;
	import mx.events.TreeEvent;
	
	[Event(name="instanceExpand",type="render.tree.NodeExpandEvent")]
	
	[Event(name="categoryExpand",type="render.tree.NodeExpandEvent")]
	
	[Event(name="attributeExpand",type="render.tree.NodeExpandEvent")]
	
	public class TreeStructureRender extends Tree
	{
		[Embed("/img/attr.png")]
		public static var ICON_ATTR:Class;
		
		[Embed("/img/cat.png")]
		public static var ICON_CAT:Class;
		
		[Embed("/img/ins.png")]
		public static var ICON_INS:Class;
		
		private var _showEdgeName:Boolean = false;
		private var _fetcherFunction:Function = defaultFetcherFunction;
		
		private var _insPageSize:int = 50;
		private var _catPageSize:int = 50;
		
		private var _formateFunction:Function = defaultFormatFunction;
		private var _expandOnInstanceNode:Boolean = false;
		private var _displayEdge:Boolean = false;
		
		public function TreeStructureRender()
		{
			this.itemRenderer = new TreeItemRenderFactory();
			this.dataDescriptor = new StructureDescriptor(this);
			this.iconFunction = internalIconFunction;
		
			addEventListener(TreeEvent.ITEM_OPENING,lazyLoadNode);
			addEventListener(ListEvent.ITEM_CLICK,clickHandler);
		}
		private function clickHandler(event:ListEvent):void
		{
			var node:INode = INode(event.itemRenderer.data);
			if(node.getNodeType() == StructureDescriptor.NODE_TYPE_CATEGORY){
				trace("category clicked");
				dispatchEvent(new NodeExpandEvent("categoryExpand",CategoryNode(node).getURI()));
			}
			else if( node.getNodeType() == StructureDescriptor.NODE_TYPE_INSTANCE){
				dispatchEvent(new NodeExpandEvent("instanceExpand",InstanceNode(node).getURI()));
			}
			else if( node.getNodeType() == StructureDescriptor.NODE_TYPE_ATTRIBUTE){
				dispatchEvent(new NodeExpandEvent("attributeExpand",AttributeNode(node).getURI()));
			}
		}
		private function internalIconFunction(data:Object):Class
		{
			var node:INode = INode(data);
			
			if(node is RootNode)
				return null;
			else if(node.getNodeType() == StructureDescriptor.NODE_TYPE_CATEGORY)
				return ICON_CAT;
			else if(node.getNodeType() == StructureDescriptor.NODE_TYPE_INSTANCE)
				return ICON_INS;
			else if(node.getNodeType() == StructureDescriptor.NODE_TYPE_ATTRIBUTE)
				return ICON_ATTR;
			else if(node.getNodeType() == StructureDescriptor.NODE_TYPE_PAGECONTORL)
				return null;
				
			return null;
		}
		
		private function lazyLoadNode(event:TreeEvent):void
		{
			var node:INode = INode(event.item);
			node.fetch();
		}
		public function set displayEdge(display:Boolean):void
		{
			_displayEdge = display;
		}
		public function get displayEdge():Boolean
		{
			return _displayEdge;
		}
		public function set rootURI(uri:String):void
		{
			dataProvider = new RootNode(this,uri,null);
		}
		public function get expandOnInstance():Boolean
		{
			return _expandOnInstanceNode;
		}
		public function set expandOnInstance(expand:Boolean):void
		{
			_expandOnInstanceNode = expand;
		}
		public function get categoryPageSize():int
		{
			return _catPageSize;
		}
		public function set categoryPageSize(pageSize:int):void
		{
			_catPageSize = pageSize;
		}
		public function set instancePageSize(pageSize:int):void
		{
			_insPageSize = pageSize;
		}
		public function get instancePageSize():int
		{
			return _insPageSize ;
		}
		public function get fetcherFunction():Function
		{
			return _fetcherFunction;
		}
		public function set fetcherFunction(func:Function):void
		{
			_fetcherFunction = func;
		}
		public function set formatFunction(func:Function):void
		{
			_formateFunction = func;
		}
		public function get formatFunction():Function
		{
			return _formateFunction;
		}
		
		private function defaultFormatFunction(uri:String):String
		{
			return uri;
		}
		private function defaultFetcherFunction(uri:String,index:int,pageSize:int):Array
		{
			var array:Array = new Array();
			return array;
		}
	}
}